// app.js
import {
  baseUrl
} from "./utils/request";
import {
  loadingShow,
  loadingHide
} from "./utils/util";

//更新
import updateManager from "./utils/updateManager"
import Scoket from "./api/scoket"
import EventEmitter from "./utils/event"; // 事件总线
App({
  towxml: require('./miniprogram_npm/towxml/index'),
  pagePath: "/pages/index/index", //当前页面路径
  onLaunch() {
    const that = this;
    updateManager()
    // 展示本地存储能力 
    const logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)


    const systemInfo = wx.getSystemInfoSync();
    that.globalData.navBarHeight = systemInfo.statusBarHeight + 44;
    // 胶囊按钮位置信息
    const menuButtonInfo = wx.getMenuButtonBoundingClientRect();


    that.globalData.menuRight = systemInfo.screenWidth - menuButtonInfo.right;
    that.globalData.menuTop = menuButtonInfo.top;
    that.globalData.menuHeight = menuButtonInfo.height;

    // 登录
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
      }
    })
  },
  onShow() {
    this.setWebScoket()
  },
  event: new EventEmitter(),
  ws: null,
  setWebScoket() {
    if (this.globalData.isClose) {
      if (this.ws && this.ws.close) this.ws.close()
      this.ws = new Scoket()
    }
  },

  /*   close() {
      console.log('111')
      this.ws.close()
    }, */

  setWsIsClose(flag) {
    this.globalData.isClose = flag
  },

  handleAdvice(url) {
    let file_path = url
    //查看电子处方
    wx.showLoading({
      title: '加载中...',
      success: () => {
        wx.downloadFile({
          url: file_path,
          success: (res) => {
            const filePath = res.tempFilePath
            wx.hideLoading()
            this.openView(filePath)
          },
          fail: () => {
            wx.showToast({
              title: '文件下载失败！',
              icon: "error"
            })
          }
        })
      }
    })
  },
  openView(path) {
    wx.openDocument({
      filePath: path,
      success: function (res) {

      },
      fail: () => {
        wx.showToast({
          title: '文件打开失败！',
          icon: "error"
        })
      }
    })
  },


  setPath(url, flag = false) {
    if (url) {
      return this.globalData.$path + url
    }
    if (flag) {
      return "/assets/common/医生.png"
    }
    return ""
  },

  isToken() {
    return !!wx.getStorageSync('token')
  },

  setToken(token, userInfo) {
    this.globalData.token = token
    this.globalData.userInfo = userInfo
    wx.setStorageSync('token', token)
    wx.setStorageSync("userInfo", userInfo)
  },
  setUserInfo(options) {
    this.globalData.userInfo = options
    wx.setStorageSync('userInfo', options)
  },
  setUserInfoPhone(options) {
    this.globalData.userInfo.phone = options
    wx.setStorageSync('userInfo.phone', options)
  },




  async getUserInfo() {
    if (!this.globalData.userInfo) {
      let data = wx.getStorageSync('userInfo')
      this.globalData.userInfo = data
    }
    return Promise.resolve(this.globalData.userInfo)
  },

  globalData: {
    loadingShow: loadingShow,
    loadingHide: loadingHide,
    use_name: null,
    password: null,
    token: "",
    baseUrl: baseUrl,
    commonUpdate: '/api/user/user/upload',
    commonUploadDoctor: '/api/doctor/upload',
    commonUpload: '/storage/upload/',
    $path: baseUrl + "/storage/upload/",
    isClose: true,
    userInfo: {},
    webData: {},
    menuRight: 0,
    menuTop: 0,
    menuHeight: 0,
  }


})